package com.mfangsoft.zhuangjialong.integration.user.service.impl;



import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.MD5Util;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.commerce.mapper.CommerceEntityMapper;
import com.mfangsoft.zhuangjialong.integration.enums.UserState;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserInfoEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserPermissionEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserRelationEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.user.model.UserInfoEntity;
import com.mfangsoft.zhuangjialong.integration.user.model.UserPermissionEntity;
import com.mfangsoft.zhuangjialong.integration.user.model.UserRelationEntity;
import com.mfangsoft.zhuangjialong.integration.user.service.UserService;
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserEntityMapper userEntityMapper;
	
	@Autowired
	private UserInfoEntityMapper userInfoEntityMapper;
	
	
	@Autowired
	private UserPermissionEntityMapper userPermissionEntityMapper;
	
	
	@Autowired
	private UserRelationEntityMapper relationEntityMapper;
	
	
	@Autowired
	private  CommerceEntityMapper commerceEntityMapper;

	@Override
	public Boolean updateUserState(Long id,Integer  value) throws Exception {
		// TODO Auto-generated method stub
		
		UserEntity userEntity = new UserEntity();
		
		userEntity.setId(id);
		
		userEntity.setState(value);
		
		userEntityMapper.updateByPrimaryKeySelective(userEntity);
		return true;
	}
	
	@Override
	public Boolean updateUserUpAndDown(Long id,Integer value)  {
		// TODO Auto-generated method stub
		
		UserEntity userEntity = new UserEntity();
		
		userEntity.setId(id);
		
		userEntity.setUp_down(value);
		
		userEntityMapper.updateByPrimaryKeySelective(userEntity);
		return true;
	}



	@Override
	public Boolean modifyPassword(Long id, String oldPwd, String newPwd, ResponseMessage<String> message)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserEntity getUserByAccount(String account) {
		// TODO Auto-generated method stub
		
		UserEntity userEntity=userEntityMapper.getUserByAccount5(account);
		
		return userEntity;
	}
	@Override
	public UserEntity getUserByAccount1(String account) {
		// TODO Auto-generated method stub
		
		UserEntity userEntity=userEntityMapper.getUserByAccount1(account);
		
		return userEntity;
	}
	@Override
	public UserEntity getUserByAccount2(String account) {
		// TODO Auto-generated method stub
		
		UserEntity userEntity=userEntityMapper.getUserByAccount2(account);
		
		return userEntity;
	}

	@Override
	public Boolean resetpassword(Long id) {
		// TODO Auto-generated method stub
		
		UserEntity userEntity  = new UserEntity();
		
		userEntity.setPwd(MD5Util.MD5("888888"));
		
		userEntity.setId(id);
		
		
		userEntityMapper.updateByPrimaryKeySelective(userEntity);
		return true;
	}

	@Override
	public List<Map<String, Object>> getUserPermissionByUserId(UserEntity userEntity) {
		// TODO Auto-generated method stub
		
		UserEntity  levelUser=null;
		
		Map<String,Object> map = new HashMap<>();
		
		
		if(userEntity.getId().longValue()!=153){
		
		if(userEntity.getUserRelationEntity().getLevelUser(3)!=null){
			
			
			
			
			levelUser=userEntity.getUserRelationEntity().getLevelUser(3);
			
			map.put("userId", levelUser.getId());
			
			map.put("level",userEntity.getUser_level()+"");
			return userPermissionEntityMapper.getUserPermissionByUserId(map);
		}else if(userEntity.getUserRelationEntity().getLevelUser(2)!=null){
			
			
			levelUser=userEntity.getUserRelationEntity().getLevelUser(2);
			
			map.put("userId", levelUser.getId());
			
			map.put("level",userEntity.getUser_level()+"");
			return userPermissionEntityMapper.getUserPermissionByUserId(map);
		}else if(userEntity.getUserRelationEntity().getLevelUser(1)!=null){
			
			levelUser=userEntity.getUserRelationEntity().getLevelUser(2);
			
			map.put("userId", levelUser.getId());
			
			map.put("level",userEntity.getUser_level()+"");
			return userPermissionEntityMapper.getUserPermissionByUserId(map);
		}
		}else{
			map.put("userId", userEntity.getId());
			return userPermissionEntityMapper.getUserPermissionByUserId(map);
		}
		return null;
		
		
		
	}

	@Override
	public UserEntity getUserById(Long userId) {
		// TODO Auto-generated method stub
		UserEntity userEntity=userEntityMapper.selectByPrimaryKey(userId);
		
		userEntity.setUserInfoEntity(userInfoEntityMapper.getUserInfoByUserId(userId));
		
		if(userEntity.getId().longValue()!=153){
			
			UserInfoEntity userInfoEntity=userInfoEntityMapper.getUserInfoByUserId(userEntity.getId());
			
			userEntity.setUserInfoEntity(userInfoEntity);
			
			
			
			
			 Map<String,Object> mapsql = new HashMap<>();
			
			if(userEntity.getUser_level().intValue()==3){
				
				mapsql.put("where_sql", "node3="+userEntity.getId()+" and  level="+userEntity.getUser_level());
			}else if(userEntity.getUser_level().intValue()==2){
				mapsql.put("where_sql", "node2="+userEntity.getId()+" and  level="+userEntity.getUser_level());
			}else if(userEntity.getUser_level().intValue()==1)
			{
				mapsql.put("where_sql", "node1="+userEntity.getId()+" and  level="+userEntity.getUser_level());
			}
			
			UserRelationEntity userRelationEntity  = relationEntityMapper.selectUserRelationByLevel(mapsql);
			
			Map<Integer,UserEntity> map = new HashMap<>();
			
			
			 UserEntity  value3=userEntityMapper.selectByPrimaryKey(userRelationEntity.getNode3());
			 
			 value3.setUserInfoEntity(userInfoEntityMapper.getUserInfoByUserId(value3.getId()));
			 map.put(3, value3);
			 
			 UserEntity  value1=userEntityMapper.selectByPrimaryKey(userRelationEntity.getNode1());
			 
			 value1.setUserInfoEntity(userInfoEntityMapper.getUserInfoByUserId(value1.getId()));
			 map.put(1, value1);
			 
			 UserEntity  value2=userEntityMapper.selectByPrimaryKey(userRelationEntity.getNode2());
			 
			 value3.setUserInfoEntity(userInfoEntityMapper.getUserInfoByUserId(value2.getId()));
			 map.put(2, value2);
			
			 userRelationEntity.setLevelUser(map);
			
			 userEntity.setUserRelationEntity(userRelationEntity);
			
			}
		return userEntity;
	}

	@Override
	public List<Map<String,Object>> getUserByLevel(Integer level,Long userId, Map<String, Object> imap,Integer leve) {
		// TODO Auto-generated method stub
		Map<String,Object> map= new HashMap<>();
		
		if(level.intValue()==1){
			
			map.put("psql", "t.node1="+userId+" and t.level="+leve);
		}
		if(level.intValue()==2){
			
			map.put("psql", "t.node2="+userId+" and t.level="+leve);
		}
		if(level.intValue()==3){
	
			map.put("psql", "t.node3="+userId+" and t.level="+leve);
		}
//		level =1;//所有 1级店铺
		
		if(imap!=null){
		map.put("province", imap.get("province"));
		map.put("city", imap.get("city"));
		map.put("district", imap.get("district"));
		}
		return relationEntityMapper.selectUserByLevel(map);
	}

	@Override
	public List<Map<String, Object>> getPermissionALL() {
		// TODO Auto-generated method stub
		return userPermissionEntityMapper.getPermissionList();
	}

	@Override
	public ResponseMessage<String> addUser(UserEntity userEntity) {
		// TODO Auto-generated method stub
		ResponseMessage<String> message = new ResponseMessage<String>();
		if(userEntity.getUser_level()==1){
			Integer departmentCode = userEntityMapper.selectDepartmentCode(userEntity.getDepartment_code());
			if(departmentCode>0){
				message.setCode("2");
				message.setMessage("部门号已存在！");
				return message;
			}
		}
		if(userEntity.getUser_level()==2){
			Integer organizationCode = userEntityMapper.selectOrganizationCode(userEntity.getOrganization_code());
			if(organizationCode>0){
				message.setCode("3");
				message.setMessage("组织编号已存在！");
				return message;
			}
		}
		userEntity.setPwd(MD5Util.MD5(userEntity.getPwd()));
		
		
		

		userEntity.getUserInfoEntity().setCreate_time(new Date());
		
		userEntity.getUserInfoEntity().setCreater(UserContext.getCurrentUserId()+"");
		commerceEntityMapper.insertSelective(userEntity.getCommerceEntity());
		
		userEntity.getUserInfoEntity().setCommerce_id(userEntity.getCommerceEntity().getId());
		userEntityMapper.insertSelective(userEntity);
		
		userEntity.getUserInfoEntity().setSys_user_id(userEntity.getId());
		
		if(userEntity.getUserInfoEntity().getApp_id()!=null&&userEntity.getUserInfoEntity().getApp_secret()!=null){
			
			
			userEntity.getUserInfoEntity().setApplication(MD5Util.MD5(userEntity.getUserInfoEntity().getApp_id()+userEntity.getUserInfoEntity().getApp_secret()));
			
		}
		
		
		userInfoEntityMapper.insertSelective(userEntity.getUserInfoEntity());
		
		
		UserRelationEntity  userRelationEntity = new UserRelationEntity();
		
		
		if(userEntity.getUser_level().intValue()==3){
			
			userRelationEntity.setNode3(userEntity.getId());
			userRelationEntity.setNode2(userEntity.getId());
			userRelationEntity.setNode1(userEntity.getId());
			userRelationEntity.setLevel(userEntity.getUser_level());
			
			
		}else if(userEntity.getUser_level().intValue()==2){
			
			if(UserContext.getCurrentUserId().longValue()==153){
				
				userRelationEntity.setNode3(userEntity.getId());
				userRelationEntity.setNode2(userEntity.getId());
				userRelationEntity.setNode1(userEntity.getId());
				userRelationEntity.setLevel(userEntity.getUser_level());
			}else{
				
				userRelationEntity.setNode3(UserContext.getCurrentUserId());
				userRelationEntity.setNode2(userEntity.getId());
				userRelationEntity.setNode1(userEntity.getId());
				userRelationEntity.setLevel(userEntity.getUser_level());
			}
			
		}else if(userEntity.getUser_level().intValue()==1){
			
			if(UserContext.getCurrentUserId().longValue()==153){
				
				userRelationEntity.setNode3(userEntity.getId());
				userRelationEntity.setNode2(userEntity.getId());
				userRelationEntity.setNode1(userEntity.getId());
				userRelationEntity.setLevel(userEntity.getUser_level());
			}else if(UserContext.getCurrentUser().getUser_level().intValue()==2){
				
				userRelationEntity.setNode3(UserContext.getCurrentUser().getUserRelationEntity().getNode3());
				userRelationEntity.setNode2(UserContext.getCurrentUserId());
				userRelationEntity.setNode1(userEntity.getId());
				userRelationEntity.setLevel(userEntity.getUser_level());
			}else if(UserContext.getCurrentUser().getUser_level().intValue()==3){
				
				userRelationEntity.setNode3(UserContext.getCurrentUserId());
				userRelationEntity.setNode2(UserContext.getCurrentUserId());
				userRelationEntity.setNode1(userEntity.getId());
				userRelationEntity.setLevel(userEntity.getUser_level());
			}
			
		}
		
		relationEntityMapper.insertSelective(userRelationEntity);
		List<UserPermissionEntity> userPermissionEntities=userEntity.getUserPermissionEntity();
		
		for (UserPermissionEntity userPermissionEntity : userPermissionEntities) {
			
			userPermissionEntity.setUser_id(userEntity.getId());
			userPermissionEntityMapper.insertSelective(userPermissionEntity);
		}
		message.setCode(SysConstant.SUCCESS_CODE);
		message.setMessage(SysConstant.SUCCESS_MSG);
		return message;
	}

	@Override
	public ResponseMessage<String> modifyUser(UserEntity userEntity) {
		// TODO Auto-generated method stub
		
		ResponseMessage<String> message = new ResponseMessage<String>();
//		userEntity.setPwd(null);
		if(userEntity.getUser_level()!=null && userEntity.getUser_level()==1){
			Map<String,Object> map=new HashMap<>();
			map.put("id", userEntity.getId());
			map.put("department_code", userEntity.getDepartment_code());
			Integer departmentCode = userEntityMapper.selectDepartmentCode1(map);
			if(departmentCode>0){
				message.setCode("2");
				message.setMessage("部门号已存在！");
				return message;
			}
		}
		if(userEntity.getUser_level()!=null && userEntity.getUser_level()==2){
			Map<String,Object> map=new HashMap<>();
			map.put("id", userEntity.getId());
			map.put("organization_code", userEntity.getOrganization_code());
			Integer organizationCode = userEntityMapper.selectOrganizationCode1(map);
			if(organizationCode>0){
				message.setCode("3");
				message.setMessage("组织编号已存在！");
				return message;
			}
		}
		
		userEntity.getUserInfoEntity().setUpdater(UserContext.getCurrentUserId()+"");
		
		userEntity.getUserInfoEntity().setUpdate_time(new Date());
		
		if(userEntity.getCommerceEntity()!=null){
		
		commerceEntityMapper.updateByPrimaryKey(userEntity.getCommerceEntity());
		}
		
		
		if(userEntity.getId()!=null){
			String pwd = userEntity.getPwd();
			if(pwd!=null && !"".equals(pwd)){
				String md5 = MD5Util.MD5(pwd);
				userEntity.setPwd(md5);
			}
			userEntityMapper.updateByPrimaryKeySelective(userEntity);
		}
		 
		
		
		if(userEntity.getUserInfoEntity().getApp_id()!=null&&userEntity.getUserInfoEntity().getApp_secret()!=null){
			
			
			userEntity.getUserInfoEntity().setApplication(MD5Util.MD5(userEntity.getUserInfoEntity().getApp_id()+userEntity.getUserInfoEntity().getApp_secret()));
			
		}
		userInfoEntityMapper.updateByPrimaryKeySelective(userEntity.getUserInfoEntity());
		List<UserPermissionEntity> userPermissionEntities=userEntity.getUserPermissionEntity();
		
		if(userPermissionEntities!=null&&!userPermissionEntities.isEmpty()){
		
		userPermissionEntityMapper.deletePermissionByUserId(userEntity.getId());
		
		
		for (UserPermissionEntity userPermissionEntity : userPermissionEntities) {
			
			userPermissionEntity.setId(null);
			userPermissionEntity.setUser_id(userEntity.getId());
			userPermissionEntityMapper.insertSelective(userPermissionEntity);
		}
		}
		message.setCode(SysConstant.SUCCESS_CODE);
		message.setMessage(SysConstant.SUCCESS_MSG);
		return message;
	}

	@Override
	public UserEntity getUsserById(Long id) {
		// TODO Auto-generated method stub
		
		UserEntity  userEntity=userEntityMapper.selectByPrimaryKey(id);
		
		
		userEntity.setUserInfoEntity(userInfoEntityMapper.getUserInfoByUserId(userEntity.getId()));
		
		userEntity.setUserPermissionEntity(userPermissionEntityMapper.getPermissionByUserId(userEntity.getId()));
		
		userEntity.setCommerceEntity(commerceEntityMapper.selectByPrimaryKey(userEntity.getUserInfoEntity().getCommerce_id()));;
		
		return userEntity;
	}

	@Override
	public Page<Map<String, Object>> queryUserList(Page<Map<String, Object>> page) {
		// TODO Auto-generated method stub
		
		
		Map<String,Object> param=(Map<String, Object>) page.getParam();
		
		if(UserContext.getCurrentUserId().longValue()==153){
			
			param.put("user_id", UserContext.getCurrentUserId()+"");
			
		}else{
			param.put("user_id", UserContext.getCurrentUserId()+"");
			param.put("create_user_id", UserContext.getCurrentUserId());
			
			param.put("user_sql", UserContext.getCurrentUserId());
			
		}
		
		
		//param.put("user_id", UserContext.getCurrentUserId()+"");
		
		page.setData(userEntityMapper.selectUserForPage(page));
		
		
		
		
		return page;
	}

	@Override
	public UserEntity getUserInfoById(Long  id) {
		// TODO Auto-generated method stub
		
		
		UserEntity  userEntity =userEntityMapper.selectByPrimaryKey(id);
		
		userEntity.setUserInfoEntity(userInfoEntityMapper.getUserInfoByUserId(id));
		
		userEntity.setCommerceEntity(commerceEntityMapper.selectByPrimaryKey(userEntity.getUserInfoEntity().getCommerce_id()));
		
		
		userEntity.setUserPermissionEntity(userPermissionEntityMapper.getPermissionByUserId(id));
		
		return userEntity;
	}

	/**
	 * 创建商户查询商户下所有下级商户的标签
	 */
	@Override
	public List<String> selectShopTag() {
		UserEntity user = UserContext.getCurrentUser();
		if(user!=null){
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("level", user.getUser_level());
			map.put("shopId",user.getId());
			List<String> shopTag = userEntityMapper.selectShopTag(map);
			return shopTag;
		}
		return null;
	}


	
}
